<?php
	require_once('../utils/logged_in.php');
	require_once('../utils/query.php');
	
	$range = $_POST['range'];
	$name_filter = $_POST['name_filter'];
	$type_filter = $_POST['type_filter'];
	$sort_by = $_POST['sort_by'];
	
	$from = $_POST['from'];
	
	$filter = '';
	$join = '';
	$columns = '';
	$unique = 0;
	
	if ($from)
	{
		if ($from == "all")
		{
			$join = '';
		}
		else if ($from == "my collection")
		{
			$join = " INNER JOIN Possession ON Possession.cardid=cards_lu.full_id";
			$filter = $filter . " AND userid = $drupaluid";
			// test - This will return userid 236's cards
		//	$filter = $filter . " AND userid = 236";
			// end test
			$columns = $columns . ", possessionid";
			$unique = 4;
		}
	}
	
	if ($name_filter)
	{
		$name_filter = str_replace(
			array(' 12', ' 11', ' 10', ' 9', ' 8', ' 7', ' 6', ' 5', ' 4', ' 3', ' 2', ' 1'),
			array('', '', '', '', '', '', '', '', '', '', '', ''),
			$name_filter);
		$filter = $filter . " AND name LIKE '%" . mysql_real_escape_string($name_filter) . "%'";
	}
	if ($type_filter)
		$filter = $filter . " AND type = " . mysql_real_escape_string($type_filter);
	if ($sort_by)
		$filter = $filter . " ORDER BY " . mysql_real_escape_string($sort_by);
	if ($range)
		$filter = $filter . " LIMIT " . mysql_real_escape_string($range);
	
	$cards_result = execute_query_unsafe("SELECT full_id, name, card_img, type" . $columns . " FROM cards_lu" . $join . " WHERE front IS NULL" . $filter);
	/* One possible query for the line above if $from == "my collection":
	  SELECT full_id, name, card_img, type '', possessionid FROM cards_lu INNER JOIN Possession ON Possession.cardid=cards_lu.full_id WHERE front IS NULL AND userid = $drupaluid
	*/
	
	//if (mysql_num_rows($cards_result) == 0)
	//	die('<ul><li id="none">There are no cards in your collection, but you can buy some at the store!</li></ul>');
?>

<!--<ul class="cards">-->
<ul>
	<?php
		while ($row = mysql_fetch_array($cards_result))
		{
			$cardid = $row[$unique];
			$name = $row[1];
			$card_img = $row[2];
			$type = $row[3];
			
			$name = str_replace(array(' 11', ' 7', ' 3', ' 2', ' 4'), array(' 9', ' 5', ' 1', ' 1', ' 2'), $name);
			
			$color = "#ffffff";
			if ($type == 1)
				$color = "#dc3122";
			else if ($type == 2)
				$color = "#ddd327";
			else if ($type == 3)
				$color = "#0145f3";
			else if ($type == 10)
				$color = "#653b67";
				
			if(strlen($name) > 16)
			$showname = substr ($name,0,16) . "...";
			else
			$showname = $name;
	?>
			<!-- '..' represents the parent directory. Images are being pulled of the ftp server -->
			<li class="card" id="<?php echo($cardid); ?>">
				<div class="cardImg">
					<img src="../../../card_imgs/<?php echo($card_img); ?>"> 
				</div>
				<!--<div class="cardDesc" style="background:<?php echo($color); ?>;">-->
				<div class="cardDesc" title="<?php echo($name); ?>">
					<?php echo($showname); ?>
				</div>
			</li>
	<?php
		}
	?>
</ul>
